{include file='public/header' /}

<body>

<div class="x-nav">

  <span class="layui-breadcrumb">

      <a href="">首页</a>
      <a href="">城市管理</a>
      <a><cite>城市列表</cite></a>
      
  </span>

  <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">

      <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>

  </a>

</div>





<div class="x-body">



    <table class="layui-hide" id="test" lay-filter="test"></table>

   <input id="currid"  type="hidden" name="curr" value="1">

</div>





<script type="text/html" id="toolbarDemo">

     
      <div class="layui-form-item"> 

        <form class="layui-form" action="" lay-filter="search">
           <input type="text" placeholder="请输入关键字搜索"  autocomplete="off" class="layui-btn layui-btn-primary" name="name">

            <div class="layui-inline">
              <button class="layui-btn"  lay-event="searchBarFunc"><i class="layui-icon">&#xe615;</i></button>
            </div>  
        </form>
      </div>
</script>



<script type="text/html" id="barDemo">

  <div class="layui-btn-group">
      
      <button type="button" class="layui-btn layui-btn-danger" lay-event="del">删除</button>
     

  </div>

</script>



<script type="text/html" id="switchTp">

      <input type="checkbox" name="禁用" value="{{d.CRI_ID}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="sxjFunc" {{ d.CRI_DATA_STATE == 1 ? 'checked' : '' }}>

</script>



<script type="text/html" id="roleinfo">

    {{#  if(d.CRI_LEVEL == 1){ }}

      <span class="layui-btn  layui-btn-xs">省级</span>

    {{#  } }}


    {{#  if(d.CRI_LEVEL == 2){ }}

      <span class="layui-btn layui-btn-normal layui-btn-xs">市级</span> 

    {{#  } }}


    {{#  if(d.CRI_LEVEL == 3){ }}

      <span class="layui-btn layui-btn-danger layui-btn-xs">县级</span> 

    {{#  } }}

</script>



<script type="text/html" id="upname">

    {{#  if(d.CRI_LEVEL > 1){ }}

      {{d.upinfo.CRI_NAME}}

    {{#  } }}

</script>



<script type="text/html" id="name">

    {{#  if(d.CRI_LEVEL == 1){ }}

      |{{d.CRI_NAME}}

    {{#  } }}


    {{#  if(d.CRI_LEVEL == 2){ }}

      |——{{d.CRI_NAME}}

    {{#  } }}

    {{#  if(d.CRI_LEVEL == 3){ }}

      |————{{d.CRI_NAME}}

    {{#  } }}


</script>






<style type="text/css">

  /*{# 设置table每一行的height #}*/

  .layui-table-cell {

   height: 50px;

   line-height: 50px;

  }

 </style>



<script>

layui.use('table', function(){

  var table = layui.table

  ,form = layui.form;

  //第一个实例

  table.render({

    elem: '#test'

    ,height:'full-100'

    ,title:'城市列表'

    ,url: "{:url('gList')}" //数据接口

    ,page: true //开启分页

    ,limit:30

    ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板

    ,cols: [[ //表头

       {field: 'CRI_NAME', title: '城市名称', templet: '#name'}
       ,{field: 'CRI_SUPERIOR_CODE', title: '上级名称', templet: '#upname'}
       ,{field: 'CRI_LEVEL', title: '级别',templet: '#roleinfo'}

       ,{field: 'CRI_DATA_STATE',  title: '启用/禁用', templet: '#switchTp',sort: true, unresize: true}

       ,{field: 'base', title: '城市价格基数', sort: true, edit:'text'}

    ]]
    ,done: function(res, curr, count){
      
      $('#currid').val(curr);
      //得到当前页码
      console.log(curr); 
    
  }
  });




  //监听上下架操作

  form.on('switch(sxjFunc)', function(obj){

    var is_xj = obj.elem.checked?1:0;

    var is_xj_str = obj.elem.checked?'已启用':'已禁用';

    var curr = $('#currid').val();

    var index = layer.load();

    $.post("{:url('editSxj')}",{id:this.value,is_xj:is_xj},function (res) {
            layer.close(index);
            if (res == 1) {

                var data = form.val('search');
              
                table.reload('test',{
                    url: "{:url('gList')}",

                    page: {
                      curr: curr 
                    }

                   ,where: data
                   ,done: function(res, curr, count){
                      form.val('search', data);
                      $('#currid').val(curr);
                      //得到当前页码
                      console.log(curr); 
                   }
                })

                layer.msg(is_xj_str+'成功',{icon:1,time:2000});
                // layer.tips(is_xj_str, obj.othis);
            }else{

                if (is_xj == 1) {
                    obj.elem.checked = false;
                }else{
                    obj.elem.checked = true;
                }
                layer.tips('暂无权限', obj.othis);
                form.render('checkbox')
            }
                

    })

  });




 //头工具栏事件

  table.on('toolbar(test)', function(obj){

       if(obj.event === 'searchBarFunc'){

              var data = form.val('search');

              // var curr = $('#currid').val();
            
              table.reload('test',{
                  url: "{:url('gList')}",

                  page: {
                    curr: 1 //重新从第 1 页开始
                  }

                 ,where: data
                 ,done: function(res, curr, count){
                    form.val('search', data);

                    $('#currid').val(curr);
                    //得到当前页码
                    console.log(curr); 

                 }
              })


        }

  });





  //监听单元格编辑
  table.on('edit(test)', function(obj){

    var value = obj.value //得到修改后的值

    ,data = obj.data //得到所在行所有键值

    ,field = obj.field; //得到字段

    if(field=='base'){

        var old=$(this).prev().text();

        var curr = $('#currid').val();

        if(!/^[+-]?(0|([1-9]\d*))(\.\d+)?$/.test(value)){

            layer.msg('请输入数字或者小数！！!',{icon:2,time:2000});
            
            $(this).val(old);

            return;

        }

        var index = layer.load();

        $.post("{:url('editBase')}",{id:data.CRI_ID,base:value},function (res) {
                layer.close(index);
                if(res == 1){

                    var data = form.val('search');
                  
                    table.reload('test',{
                        url: "{:url('gList')}",

                        page: {
                          curr: curr 
                        }

                       ,where: data
                       ,done: function(res, curr, count){
                          form.val('search', data);
                          $('#currid').val(curr);
                       }
                    })

                    layer.msg('更改成功', {icon: 1});

                }else {

                      var data = form.val('search');
                  
                      table.reload('test',{
                          url: "{:url('gList')}",

                          page: {
                            curr: curr 
                          }

                         ,where: data
                         ,done: function(res, curr, count){
                            form.val('search', data);
                            $('#currid').val(curr);
                         }
                      })
                      layer.msg('暂无权限',{icon:2,time:2000});
                     
                      return;

                }

            })

    }


  });




  

});

</script>











</body>

</html>